package com.taptech.doufu.util;

import com.taptech.doufu.constant.Constant;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.SharedPreferences;
import android.net.Uri;
import android.provider.Settings;
import android.provider.Settings.SettingNotFoundException;
import android.view.WindowManager;

public class ScreenBrightnessUtil {
	/** 
	 * 判断是否开启了自动亮度调节 
	 *  
	 * @param
	 * @return 
	 */  
	public static boolean isAutoBrightness(ContentResolver aContentResolver) {  
	    boolean automicBrightness = false;  
	    try {  
	        automicBrightness = Settings.System.getInt(aContentResolver,  
	                Settings.System.SCREEN_BRIGHTNESS_MODE) == Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC;  
	    } catch (SettingNotFoundException e) {  
	        e.printStackTrace();  
	    }  
	    return automicBrightness;  
	}  
	  
	/** 
	 * 获取屏幕的亮度 
	 *  
	 * @param activity 
	 * @return 
	 */  
	public static int getScreenBrightness(Activity activity) {  
	    int nowBrightnessValue = 0;  
	    ContentResolver resolver = activity.getContentResolver();  
	    try {  
	        nowBrightnessValue = android.provider.Settings.System.getInt(  
	                resolver, Settings.System.SCREEN_BRIGHTNESS);  
	    } catch (Exception e) {  
	        e.printStackTrace();  
	    }  
	    return nowBrightnessValue;  
	}  
	  
	/** 
	 * 设置亮度 
	 *  
	 * @param activity 
	 * @param brightness 
	 */  
	public static void setBrightness(Activity activity, int brightness) {  
	    // Settings.System.putInt(activity.getContentResolver(),   
	    // Settings.System.SCREEN_BRIGHTNESS_MODE,   
	    // Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);   
	    WindowManager.LayoutParams lp = activity.getWindow().getAttributes();  
	    lp.screenBrightness = Float.valueOf(brightness) * (1f / 255f);  
	    activity.getWindow().setAttributes(lp);
	}  
	  
	/** 
	 * 停止自动亮度调节 
	 *  
	 * @param activity 
	 */  
	public static void stopAutoBrightness(Activity activity) {  
	    Settings.System.putInt(activity.getContentResolver(),  
	            Settings.System.SCREEN_BRIGHTNESS_MODE,  
	            Settings.System.SCREEN_BRIGHTNESS_MODE_MANUAL);
		Uri uri = android.provider.Settings.System.getUriFor("screen_brightness");
		activity.getContentResolver().notifyChange(uri, null);
	}

	/** 
	 * 开启亮度自动调节 
	 *  
	 * @param activity 
	 */  
	public static void startAutoBrightness(Activity activity) {  
	    Settings.System.putInt(activity.getContentResolver(),  
	            Settings.System.SCREEN_BRIGHTNESS_MODE,  
	            Settings.System.SCREEN_BRIGHTNESS_MODE_AUTOMATIC);
		Uri uri = android.provider.Settings.System.getUriFor("screen_brightness");
		activity.getContentResolver().notifyChange(uri, null);
	}  
	
	public static void changeDayNight(final Activity act,SharedPreferences dayNightPreferences, int systemBri){
		String dayornight = dayNightPreferences.getString(Constant.DayNight.IS_DAY, "");
		if (dayornight != null) {
			if (dayornight.equalsIgnoreCase(Constant.DayNight.DAY)) {
				setBrightness(act,30);
			}else{
				setBrightness(act, systemBri);//getScreenBrightness(act));
			}
		}
	}
	  
	/** 
	 * 保存亮度设置状态 
	 *  
	 * @param resolver 
	 * @param brightness 
	 */  
	public static void saveBrightness(ContentResolver resolver, int brightness) {  
	    Uri uri = android.provider.Settings.System  
	            .getUriFor("screen_brightness");  
	    android.provider.Settings.System.putInt(resolver, Settings.System.SCREEN_BRIGHTNESS,  
	            brightness);  
	    resolver.notifyChange(uri, null);  
	}

	// 获取系统屏幕亮度
	public static int getSysScreenBrightness(Activity activity) {
		int value = 0;
		ContentResolver cr = activity.getContentResolver();
		try {
			value = Settings.System.getInt(cr, Settings.System.SCREEN_BRIGHTNESS);
		} catch (SettingNotFoundException e) {

		}
		return value;
	}

}
